//
//  AppDelegate.swift
//  weiboOfAfan
//
//  Created by afan on 16/3/19.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        ///设置导航栏 Item 颜色
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        window = UIWindow(frame: UIScreen .mainScreen().bounds)
        
//        let tabBarVC = AfanMainController()
        
        window?.rootViewController = defaultViewController()
        
        window?.makeKeyAndVisible()
//        sleep(2)
        return true
    }
    
    ///界面切换
    private func defaultViewController() ->UIViewController {
        if !AfanUserAccountViewModel.shardInstace.userLogin{
            return AfanMainController()
        }
        return isNewVersion() ? AfanNewFeatureViewController() : AfanWelcomeController()
    }
    
    
    /// 外部调用的切换根控制器方法
    func switchRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    /// 外部调用的切换根控制器方法
    class func outSwitchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootViewController(controller)
    }
    
    
    
    
    ///返回true, 就是新版本
    private func isNewVersion() -> Bool {
        ///1当前版本号
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        ///2下一个版本号
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        
        
        // 3.对比, 不一样就是新版本
        let newVersion = version != sandboxVersion
        
        // 4.是新版本,保存当前版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 5.返回比较结果
        return newVersion
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}

